﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using WeifenLuo.WinFormsUI.Docking;
using System.Threading;

namespace SimplePad
{
    public partial class EditFile : DockContent
    {
        private FileHandler _file;

        private bool _isClosing = false;
        public bool IsClosing
        {
            get { return _isClosing; }
        }

        private int _textHashCode;

        public EditFile(FileHandler file)
        {
            InitializeComponent();

            _file = file;
            updateFileInfo();
            textEditorControl1.LoadFile(_file.FullName);
            _textHashCode = textEditorControl1.Text.GetHashCode();
            _file.Changed = false;
            _file.OnFileChange += _file_OnFileChange;
            _file.OnActivate += _file_OnActivate;
            _file.OnExternFileChange += _file_OnExternFileChange;
        }

        void _file_OnActivate(FileHandler file)
        {
            Activate();
        }

        void _file_OnExternFileChange(FileHandler file)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(showUpdateBar));
            }
            else
            {
                showUpdateBar();
            }
        }

        void _file_OnFileChange(FileHandler file)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(updateFileInfo));
            }
            else
            {
                updateFileInfo();
            }
        }

        private void showUpdateBar()
        {
            notificationBar1.Visible = true;
            notificationBar1.Flash(5);
            updateFileInfo();
        }

        private void updateFileInfo()
        {
            Text = _file.FileName;
            Icon = _file.getFileIcon();
            textEditorControl1.IsReadOnly = _file.IsReadOnly;

            _file.DockPanel.Refresh();
        }

        private void EditFile_FormClosing(object sender, FormClosingEventArgs e)
        {
            //MessageBox.Show("");
            FilesManager.CloseFile(_file.FullName);
            _isClosing = true;
            if (_file.Changed)
            {
                e.Cancel = saveDirty();
            }
        }

        private bool saveDirty()
        {
            switch (MessageBox.Show("Save ?", "File " + _file.FileName + " is dirty", MessageBoxButtons.YesNoCancel))
            {
                case DialogResult.Yes:
                    _file.Save(textEditorControl1.Text);
                    return false;
                case DialogResult.No:
                    return false;
                case DialogResult.Cancel:
                    return true;
                default:
                    return false;
            }
        }

        private void textEditorControl1_TextChanged(object sender, EventArgs e)
        {
            _file.Changed = (_textHashCode != textEditorControl1.Text.GetHashCode());
        }
    }
}
